package com.miku.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miku.blog.domain.Article;
import com.miku.blog.domain.dto.TagDTO;
import com.miku.blog.domain.pojo.ResponseResult;
import com.miku.blog.domain.Tag;
import com.miku.blog.domain.vo.PageVo;
import com.miku.blog.eume.HttpStatusEnum;
import com.miku.blog.exception.SystemException;
import com.miku.blog.mapper.ArticleMapper;
import com.miku.blog.service.ArticleService;
import com.miku.blog.service.TagService;
import com.miku.blog.mapper.TagMapper;
import com.miku.blog.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author Miku
* @description 针对表【tb_tag】的数据库操作Service实现
* @createDate 2022-05-26 19:01:03
*/
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag>
    implements TagService{

    @Lazy
    @Autowired
    private ArticleService articleService;

    @Override
    public List<Tag> getTagListByArticle(@Nullable Integer articleId) {
        List<Tag> tagList = baseMapper.getTagListByArticleId(articleId);
        return tagList;
    }

    @Override
    public ResponseResult tags(Integer current, Integer size,@Nullable String keywords) {

        Page<Tag> page = new Page<>(current, size);
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(keywords),Tag::getTagName,keywords)
                .orderByDesc(Tag::getCreateTime);
        page(page,queryWrapper);
        List<Tag> tagList = page.getRecords();
        tagList.stream()
                .peek(tag -> {
                    ArticleMapper articleMapper = (ArticleMapper) articleService.getBaseMapper();
                    long articleCount = articleMapper.getArticleCountByTagId(tag.getId());
                    tag.setArticleCount((int) articleCount);
                }).collect(Collectors.toList());
        PageVo<Tag> pageVo = new PageVo<>(page.getTotal(),tagList);
        return ResponseResult.ok().setData(pageVo);
    }


    @Transactional
    @Override
    public ResponseResult deleteTags(Integer[] tagIdList) {

        for (Integer tagId : tagIdList) {
            ArticleMapper articleMapper = (ArticleMapper)articleService.getBaseMapper();
            List<Article> articleList = articleMapper.getArticleListByTagId(tagId);
            if (Objects.isNull(articleList) || articleList.size() <= 0){
                if (!removeById(tagId)) {
                    throw new SystemException(HttpStatusEnum.REMOVE_DATA_ERROR);
                }
            }else {
                throw new SystemException(HttpStatusEnum.DELETE_TAG_ERROR);
            }
        }
        return ResponseResult.ok();
    }

    @Override
    public ResponseResult addTags(Tag tag) {
        if (!StringUtils.hasText(tag.getTagName())){
            return new ResponseResult(HttpStatusEnum.PARAM_ERROR);
        }

        Integer tagId = tag.getId();
        if (Objects.isNull(tagId)){
            //若tagId为NULL,则表示新增数据
            if (Objects.nonNull(getTagByName(tag.getTagName()))){
                return new ResponseResult(HttpStatusEnum.INSERT_TAG_ERROR);
            }
//            tag.setCreateTime(new Date());
            if (!save(tag)) {
                return new ResponseResult(HttpStatusEnum.INSERT_DATA_ERROR);
            }
        }else {
            String tagName = tag.getTagName();
            LambdaUpdateWrapper<Tag> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Tag::getId,tagId)
                    .set(Tag::getTagName,tagName);
            if (!update(updateWrapper)) {
                return new ResponseResult(HttpStatusEnum.UPDATE_DATA_ERROR);
            }
        }
        return ResponseResult.ok();
    }


    @Override
    public ResponseResult getTags() {
        List<Tag> tagList = list();
        List<TagDTO> tagDTOList = BeanCopyUtils.copyListByType(tagList, TagDTO.class);
        PageVo<TagDTO> pageVo = new PageVo<>((long) tagDTOList.size(),tagDTOList);
        return ResponseResult.ok().setData(pageVo);
    }


    @Override
    public Tag  getTagByName(String tagName){
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tag::getTagName,tagName);
        return getOne(queryWrapper);
    }


    @Override
    public ResponseResult getTagList() {
        List<Tag> tagList = list();
        List<TagDTO> tagDTOList = BeanCopyUtils.copyListByType(tagList, TagDTO.class);
        return ResponseResult.ok().setData(tagDTOList);
    }
}




